package com.artqiyi.dollmachine.views.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseFragment;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.model.entity.GameRecordBean;
import com.artqiyi.dollmachine.presenter.MineCatchRecordPresenter;
import com.artqiyi.dollmachine.presenter.OperateCatchRecordPresenter;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.CompatUtil;
import com.artqiyi.dollmachine.utils.RxRecyclerViewDividerTool;
import com.artqiyi.dollmachine.utils.ScreenUtil;
import com.artqiyi.dollmachine.views.activity.DollOperateActivity;
import com.artqiyi.dollmachine.views.activity.MineCatchRecordActivity;
import com.artqiyi.dollmachine.views.adapter.GameRecordsListAdapter;
import com.artqiyi.dollmachine.views.adapter.MineCatchRecordAdapter;
import com.artqiyi.dollmachine.views.widget.CommonFooter;
import com.artqiyi.dollmachine.views.widget.CommonHeader;
import com.artqiyi.dollmachine.views.widget.CustomGifHeader;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/29 0029
 * desc: 操作界面详情
 */

public class OperateCatchRecordFragment extends BaseFragment<OperateCatchRecordPresenter> implements PresenterCallbackCollection.OperateCatchRecordFragmentCallback{

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView mXRefreshView;

    private GameRecordsListAdapter mAdapter;

    private List<GameRecordBean> mDollBeans = new ArrayList<>();

    private int pageNum = 1;

    private int machineId;
    @Override
    protected OperateCatchRecordPresenter getPresenter() {
        return new OperateCatchRecordPresenter();
    }

    @Override
    protected void handleArguments(Bundle bundle) {
        super.handleArguments(bundle);
        machineId = getArguments().getInt(Constant.KEY_MACHINE_ID, 0);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_operate_catch_record;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new GameRecordsListAdapter(mDollBeans);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new RxRecyclerViewDividerTool(ScreenUtil.dp2px(getActivity(), 2f)));
        mRecyclerView.setAdapter(mAdapter);
        CommonHeader header = new CommonHeader(getActivity());
        header.setBackground(R.color.white);
        CommonFooter footer = new CommonFooter(getActivity());
        footer.setBackground(R.color.white);
        mXRefreshView.setCustomHeaderView(header);
        mXRefreshView.setCustomFooterView(footer);
        mXRefreshView.setAutoLoadMore(true);
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                mXRefreshView.setLoadComplete(false);
                pageNum = 1;
                mPresenter.getMineCatchRecord(machineId, pageNum);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                mPresenter.getMineCatchRecord(machineId, pageNum);
            }
        });

    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setVisibility(View.GONE);
    }

    @Override
    public void loadData() {
        super.loadData();
        pageNum = 1;
        mPresenter.getMineCatchRecord(machineId, pageNum);
    }

    @Override
    public void onGetMineCatchRecordSuccess(List<GameRecordBean> beans) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.stopLoadMore();
        }

        if (null != beans && beans.size() != 0) {
            if (pageNum == 1) {
                mDollBeans.clear();
            }
            mDollBeans.addAll(beans);
            mAdapter.notifyDataSetChanged();
            if (beans.size() < 20) {
                mXRefreshView.setLoadComplete(true);
            }
            pageNum++;
        }
    }

    @Override
    public void onGetMineCatchRecordFail(String msg) {
        if (null != mXRefreshView) {
            mXRefreshView.stopRefresh();
            mXRefreshView.setLoadComplete(true);
            Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
        }
    }
}
